package com.sts.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.sts.bean.Car;
import com.sts.bean.User1;
import com.sts.service.ICarService;

/**
 * 机器人controller层
 * @author FENG
 *
 */

@Controller
public class CarController {

	private Logger log = Logger.getLogger(this.getClass()) ;
	
	@Resource
	private ICarService icars;
	
	/**
	 * 查询所有的人员
	 */
	@RequestMapping(value="/selectCar", produces="text/html;charset=UTF-8")
	@ResponseBody
	public String selectCar(String page ,String limit ,String car_id ,String car_vs,String car_name){
		log.debug("selectCar controller in");
		
		log.debug("car_name:" + car_name + ",car_id:" + car_id);
		String str = "" ;
		List<Car> list = icars.selCar(car_id, car_name) ;
		int length = icars.selSize(car_id, car_name);
		str = JSON.toJSONString(list);
		log.debug("list:" + list);
		log.debug("length:" + length);
		if(car_vs == null){
			str= "{\"code\":0,\"msg\":\"  \",\"count\":\""+length+"\",\"data\":"+str+"}";
		}
		log.debug("selectCar controller out");
		
		return str ;
	}

	/**
	 * 根据id删除机器人
	 */
	@RequestMapping(value="/deleteCar", produces="text/html;charset=UTF-8")
	@ResponseBody
	public String deleteCar(String car_id ){
		log.debug("deleteCar controller in");
		log.debug(",car_id:" + car_id);
		String str = "" ;
		
		if(icars.deleteCar(car_id) == 1){
			str = "success" ;
		}else{
			str = "false" ;
		}
		
		
		log.debug("deleteCar controller in");
		return str ;
	}
	/**
	 * 插入car信息
	 * @param data
	 * @return
	 */
	@RequestMapping("/addCar")
	@ResponseBody
	public String addCar(String data){
		log.debug("addCar controller in");
		String result = "" ;
		log.debug("数据" + data);
		//将JSON对象转换为对象
		Car car = JSON.parseObject(data,Car.class) ;
		log.debug(car.toString());
		int b = icars.insertCar(car) ;
		if(b == 1){
			result = "success" ;
		}else {
			result = "false" ;
		}
		
		
		log.debug("addCar controller out");
		return result;
	}
	/**
	 * 插入car信息
	 * @param data
	 * @return
	 */
	@RequestMapping("/updateCar")
	@ResponseBody
	public String updateCar(String data){
		log.debug("updateCar controller in");
		String result = "" ;
		log.debug("数据" + data);
		//将JSON对象转换为对象
		Car car = JSON.parseObject(data,Car.class) ;
		log.debug(car.toString());
		int b = icars.updateCar(car) ;
		if(b == 1){
			result = "success" ;
		}else {
			result = "false" ;
		}
		
		
		log.debug("updateCar controller out");
		return result;
	}
	
	/**
	 * 访问userAdd页面
	 * 
	 */
	
	@RequestMapping("/carAdd")
	public String carAdd(){
		
		log.debug("carAdd  controller in");
		
		log.debug("carAdd  controller out");
		return "carAdd";
	}
	/**
	 * 访问car_revise页面
	 * 
	 */
	@RequestMapping(value="/car_revise", produces="text/html;charset=UTF-8")
	public String car_revise(String car_id,String car_vs,HttpSession session){
		
		log.debug("car_revise  controller in");
		log.debug("car_id "+ car_id + ",car_vs:" + car_vs);
		session.setAttribute("car_id", car_id);
		session.setAttribute("car_vs", car_vs);
		log.debug("car_revise  controller out");
		return "car_revise";
	}
	
	
}
